
--[[
title: ToolBase
description: 基类
local A = commonlib.inherit(commonlib.gettable("commonlib.ToolBase"));
A:Property("Key", "hello");
print(A:GetKey());
A:SetKey("world");
print(A:GetKey());
]]

local ToolBase = commonlib.inherit(nil, commonlib.gettable("commonlib.ToolBase"));

function ToolBase:ctor()
	self.m_class_name = "ToolBase";
end

-- 属性 setter getter
function ToolBase:Property(fieldname, default_value, getter, setter)
	local fieldkey = "__field_" .. fieldname .. "__";

	self[fieldkey] = default_value;

	getter = getter or ("Get" .. fieldname);
	setter = setter or ("Set" .. fieldname);

	self[getter] = function(this)
		return this[fieldkey];
	end

	self[setter] = function(this, value)
		this[fieldkey] = value;
	end
end

-- 触发事件
function ToolBase:TriggerEventCallBack(event_name, ...)
	local event_key = "__event_" .. event_name .. "__";
	local event_map = self[event_key] or self[event_key] or {};
	for _, callback in pairs(event_map) do
		callback(...);
	end
end

-- 监听事件
function ToolBase:RegisterEventCallBack(event_name, class_instance, member_function)
	if (type(class_instance) == "function" and member_function == nil) then
		member_function = class_instance;
		class_instance = nil;
	end

	if (type(member_function) ~= "function") then return end

	local event_key = "__event_" .. event_name .. "__";
	local callback_key = "__callback_" .. tostring(event_name) .. "_" .. tostring(class_instance) .. "_" .. tostring(member_function) .. "__";
	local callback = class_instance == nil and member_function or function (...) member_function(class_instance, ...) end
	local event_map = self[event_key] or self[event_key] or {};
	event_map[callback_key] = callback;
	self[event_key] = event_map;
end

-- 移除事件
function ToolBase:RemoveEventCallBack(event_name, class_instance, member_function)
	if (type(class_instance) == "function" and member_function == nil) then
		member_function = class_instance;
		class_instance = nil;
	end

	if (type(member_function) ~= "function") then return end

	local event_key = "__event_" .. event_name .. "__";
	local callback_key = "__callback_" .. tostring(event_name) .. "_" .. tostring(class_instance) .. "_" .. tostring(member_function) .. "__";
	local event_map = self[event_key] or self[event_key] or {};
	event_map[callback_key] = nil;
end


-- 触发事件
function ToolBase:TriggerOnceEventCallBack(event_name, ...)
	local event_key = "__once_event_" .. event_name .. "__";
	local event_map = self[event_key] or self[event_key] or {};
	for _, callback in pairs(event_map) do
		callback(...);
	end
	self[event_key] = nil;
end

-- 监听事件
function ToolBase:RegisterOnceEventCallBack(event_name, class_instance, member_function)
	if (type(member_function) ~= "function") then return end

	if (type(class_instance) == "function" and member_function == nil) then
		member_function = class_instance;
		class_instance = nil;
	end

	local event_key = "__once_event_" .. event_name .. "__";
	local callback_key = "__callback_" .. tostring(event_name) .. "_" .. tostring(class_instance) .. "_" .. tostring(member_function) .. "__";
	local callback = class_instance == nil and member_function or function (...) member_function(class_instance, ...) end
	local event_map = self[event_key] or self[event_key] or {};
	event_map[callback_key] = callback;
	self[event_key] = event_map;
end

-- 移除事件
function ToolBase:RemoveOnceEventCallBack(event_name, class_instance, member_function)
	if (type(member_function) ~= "function") then return end

	if (type(class_instance) == "function" and member_function == nil) then
		member_function = class_instance;
		class_instance = nil;
	end

	local event_key = "__once_event_" .. event_name .. "__";
	local callback_key = "__callback_" .. tostring(event_name) .. "_" .. tostring(class_instance) .. "_" .. tostring(member_function) .. "__";
	local event_map = self[event_key] or self[event_key] or {};
	event_map[callback_key] = nil;
end

-- 单例模式
function ToolBase:InitSingleton()
	local function ctor_recursive(o, current_class)
		current_class = current_class or o;
		if(current_class._super) then
			ctor_recursive(o, current_class._super);
		end
		local ctor = rawget(current_class, "ctor");
		if(type(ctor) == "function") then
			ctor(o);
		end
	end
	if(not rawget(self, "m_singleton_inited")) then
		self.m_singleton_inited = true;
		ctor_recursive(self);
		self.new = nil;
	end
	return self;
end

